package com.yanhui.utils.file.video;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description ${END}
 * @Author yanhui
 * @Date 2019-03-17 16:52
 */
public class _ffmpegUtils {


        private static final String FFMPEG_EXE = "D:\\DesignSoftware\\ffmpeg-20181023-c2ac3b8-win64-static\\bin\\ffmpeg.exe";

        public static void main(String[] args) {
            String path = "D:\\";
            File file = new File(path);
            if (file.exists()) {
                File[] files = file.listFiles();
                if (null == files || files.length == 0) {
                    System.out.println("文件夹是空的!");
                    return;
                } else {
                    for (File file2 : files) {
                        if (file2.isDirectory()) {
//                            System.out.println("文件夹:" + file2.getAbsolutePath());
//                            traverseFolder2(file2.getAbsolutePath());
                        } else {
//                            System.out.println("文件:" + file2.getAbsolutePath());
                            String absolutePath = file2.getAbsolutePath();
                            if(absolutePath.endsWith(".mp4")){
                                absolutePath = absolutePath.replace("D:\\","").replace(".mp4","");
                                int page = 6;
                                for(int i = 0;i<page;i++){
                                    getImageFromVideo2(path+absolutePath+".mp4", path+absolutePath+"_"+(i+1)+".jpg", "800*600", (i+1)+"");
                                }
                            }
                        }
                    }
                }
            } else {
                System.out.println("文件不存在!");
            }


//            String videoRealPath = "欢乐颂：曲筱绡蹲街大哭，连分手都这么可爱啊，赵医生竟然后悔了-国语720P.qsv.flv";
//            int page = 6;
//            for(int i = 0;i<page;i++){
//                getImageFromVideo2(path+videoRealPath+".mp4", path+videoRealPath+"_"+(i+1)+".jpg", "800*600", (i+1)+"");
//            }



//        public static void main(String[] args) {
//            String path = "D:\\";
//            String videoRealPath = "欢乐颂：曲筱绡蹲街大哭，连分手都这么可爱啊，赵医生竟然后悔了-国语720P.qsv.flv";
////            String videoRealPath = "欢乐颂：曲筱绡蹲街大哭，连分手都这么可爱啊，赵医生竟然后悔了-国语720P.qsv.flv.mp4";
////            String imageRealPath = "欢乐颂：曲筱绡蹲街大哭，连分手都这么可爱啊，赵医生竟然后悔了-国语720P.qsv.flv.jpg";
//            int page = 6;
//            for(int i = 0;i<page;i++){
//                getImageFromVideo2(path+videoRealPath+".mp4", path+videoRealPath+"_"+(i+1)+".jpg", "800*600", (i+1)+"");
////                try {
////                    Thread.sleep(5000);
////                } catch (InterruptedException e) {
////                    e.printStackTrace();
////                }
//            }

//            String voiceRealPath ="D:\\ffmpeg\\output\\test.mp3";
//            String videoOutputPath ="D:\\test3.mp4";
//            String picRealPath ="D:\\ffmpeg\\output\\ah%d.jpg";
////        getVoiceFromVideo(videoRealPath, voiceRealPath);
//            getMp4FromVideo(videoRealPath, videoOutputPath);
////        getPartFromVideo(videoRealPath,"00:00:00","00:01:00", videoOutputPath);
////        videoToPic(videoRealPath,picRealPath,"00:00:00","00:00:03");
//
////        picToVideo(picRealPath,videoRealPath2);
//            String mp3path = "D:\\1.mp3";
//            String mp4path = videoRealPath;
//            String mp4outpath = "D:\\test4.mp4";
////        comMP3MP4(mp3path, mp4path, mp4outpath); //不可用

        }

        public static void comMP3MP4(String mp3path, String mp4path, String mp4outpath){

            List<String> commend = new ArrayList<String>();
            commend.add(FFMPEG_EXE);
            commend.add("-i");//合成音视频
            commend.add(mp3path);
            commend.add("-i");
            commend.add(mp4path);
            commend.add("-f");
            commend.add("mp4");
            commend.add("-y");
            commend.add(mp4outpath);

            try {
                ProcessBuilder builder = new ProcessBuilder(commend);
                builder.command(commend);
                builder.redirectErrorStream(true);
                Process process = builder.start();
                process.waitFor();//等待进程执行结束
                process.destroy();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        public static boolean picToVideo(String picRealPath, String videoRealPath){

            List<String> commend = new ArrayList<String>();
            commend.add(FFMPEG_EXE);
            commend.add("-y");//图片合成
            commend.add("-r");
            commend.add("30");
            commend.add("-i");
            commend.add(picRealPath);
            commend.add("-absf");
            commend.add("aac_adtstoasc");
            commend.add(videoRealPath);

            try {
                ProcessBuilder builder = new ProcessBuilder(commend);
                builder.command(commend);
                Process process = builder.start();
                process.waitFor();//等待进程执行结束
                process.destroy();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }


        public static boolean videoToPic(String videoRealPath,String picRealPath,String starTime,String endTime){

            List<String> commend = new ArrayList<String>();
            commend.add(FFMPEG_EXE);
            commend.add("-i");//分帧
            commend.add(videoRealPath);
            commend.add(picRealPath);
            commend.add("-vcodec");
            commend.add("mjpeg");
            if(starTime != null){
                commend.add("-ss");
                commend.add(starTime);
                commend.add("-t");
                commend.add(endTime);
            }
		/*commend.add("-ss");
		commend.add(starTime);
		commend.add("-t");
		commend.add(endTime);*/
            try {
                ProcessBuilder builder = new ProcessBuilder();
                builder.command(commend);
                builder.redirectErrorStream(true);
                Process process = builder.start();
                process.waitFor();//等待进程执行结束
                process.destroy();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }


        private static void getMp4FromVideo(String videoRealPath, String videoOutputPath){
            List<String> commend = new ArrayList<String>();
            commend.add(FFMPEG_EXE);
            commend.add("-i");//获取无声视频
            commend.add(videoRealPath);
            commend.add("-vcodec");
            commend.add("copy");
            commend.add("-an");
            commend.add(videoOutputPath);
            try {
                ProcessBuilder builder = new ProcessBuilder();
                builder.command(commend);
                builder.redirectErrorStream(true);
                Process process = builder.start();
                process.waitFor();//等待进程执行结束
                process.destroy();
                //new File(oldfilepath).delete();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        //截取视频
        private static void getPartFromVideo(String videoRealPath, String starTime, String endTime,
                                             String videoOutputPath){
            List<String> commend = new ArrayList<String>();
            commend.add(FFMPEG_EXE);
            commend.add("-i");//截取视频段
            commend.add(videoRealPath);
            commend.add("-ss");
            commend.add(starTime);//start
            commend.add("-t");
            commend.add(endTime);//end
            commend.add("-acodec");
            commend.add("copy");
            commend.add("-vcodec");
            commend.add("copy");
            commend.add(videoOutputPath);

            try {
                ProcessBuilder builder = new ProcessBuilder();
                builder.command(commend);
                builder.redirectErrorStream(true);
                Process process = builder.start();
                process.waitFor();//等待进程执行结束
                process.destroy();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        private static void getVoiceFromVideo(String videoRealPath, String voiceRealPath){
            // 视频文件
//        String videoRealPath ="D:\\mp4\\test.mp4";
//        // 截图的路径（输出路径）
//        String voiceRealPath ="D:\\ffmpeg\\output\\test.mp3";
            List<String> commend =new java.util.ArrayList<String>();
            commend.add(FFMPEG_EXE);
            commend.add("-i");
            commend.add(videoRealPath);
            commend.add("-vn");
            commend.add("-ar");
            commend.add("44100");
            commend.add("-ac");
            commend.add("2");
            commend.add("-ab");
            commend.add("192");
            commend.add("-f");
            commend.add("mp3");
            commend.add(voiceRealPath);
            try {
                ProcessBuilder builder = new ProcessBuilder();
                builder.command(commend);
                builder.redirectErrorStream(true);
                System.out.println("视频语音分离开始...");
                Process process =builder.start();
                process.waitFor();//等待进程执行结束
                System.out.println("视频语音分离完成...");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("视频语音分离失败！");
            }

        }

        private static void getImageFromVideo(String videoRealPath, String imageRealPath) {
            // 视频文件
//        String videoRealPath = "D:\\2.mp4";
            // 截图的路径（输出路径）
//        String imageRealPath = "D:\\ffmpeg\\output\\test.jpg";
            List<String> commend = new java.util.ArrayList<String>();
            commend.add(FFMPEG_EXE);
            commend.add("-i");
            commend.add(videoRealPath);
            commend.add("-ss");
            commend.add("3");
            commend.add("-vframes");
            commend.add("1");
            commend.add("-r");
            commend.add("1");
            commend.add("-ac");
            commend.add("1");
            commend.add("-r");
            commend.add("1");
            commend.add("-ac");
            commend.add("1");
            commend.add("-ab");
            commend.add("2");
            commend.add("-s");
            commend.add("350*240");
            commend.add("-f");
            commend.add("image2");
            commend.add(imageRealPath);
            try {
                ProcessBuilder builder = new ProcessBuilder();
                builder.command(commend);
                builder.redirectErrorStream(true);
                System.out.println("视频截图开始...");
                Process process = builder.start();
                System.out.println("视频截图完成...");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("视频截图失败！");
            }
        }
        private static void getImageFromVideo2(String videoRealPath, String imageRealPath, String image, String second) {
            List<String> commend = new java.util.ArrayList<String>();
            commend.add(FFMPEG_EXE);
            commend.add("-i");
            commend.add(videoRealPath);
            commend.add("-ss");
            commend.add(second);
            commend.add("-vframes");
            commend.add("1");
            commend.add("-r");
            commend.add("1");
            commend.add("-ac");
            commend.add("1");
            commend.add("-r");
            commend.add("1");
            commend.add("-ac");
            commend.add("1");
            commend.add("-ab");
            commend.add("1");
            commend.add("-s");
            commend.add(image);
            commend.add("-f");
            commend.add("image2");
            commend.add(imageRealPath);
            try {
                ProcessBuilder builder = new ProcessBuilder();
                builder.command(commend);
                builder.redirectErrorStream(true);
                System.out.println("视频截图开始...");
                Process process = builder.start();
                System.out.println("视频截图完成...");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("视频截图失败！");
            }
        }


}
